package com.qingfeng.coupon.constant;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/5/26 5:14 下午
 */
@Getter
@AllArgsConstructor
public enum CouponExpireType {

    FIX_DATA(1,"固定天数"),
    FIX_DEADLINE(2,"固定日期范围");

    @JsonValue
    @EnumValue
    private Integer code;

    private String description;

//
//    @JsonValue
//    public JSONObject value(){
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code",this.getCode());
//        jsonObject.put("desc",this.getDescription());
//        return jsonObject;
//    }

    public static CouponExpireType of(Integer code){
        Objects.requireNonNull(code);
        return Stream.of(values()).
                filter(bean->bean.code.equals(code))
                .findAny()
                .orElseThrow(()->new IllegalArgumentException(code+"not exist"));
    }


    @Override
    public String toString() {
        return "{" +
                "code=" + code +
                ", description='" + description + '\'' +
                '}';
    }}
